<template>
    <div>
        <Row style="margin:5px 0px 10px 5px">
            <i-col span="1" >
                <Button type="primary" @click="btn_refresh">刷新</Button>
            </i-col>
        </Row>
        <i-table stripe border  :columns="format" :data="show_list" :height="sharedState.tableHeight" ></i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>
</template>

<script>

import { get_node_stat } from  '@/api'
import {k8s_cluster_post} from '@/api/http'
import store from '@/store'

export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                {
                    title: '节点',key: 'name',align:'center',
                },
                {
                    title: '类型',key: 'role',align:'center',
                    render: (h,params) => {
                        let value = params.row.role
                        if(value=='master'){
                            return h('p','主节点')
                        }else{
                            return h('p','工作节点')
                        }
                    }
                },

                {
                    'title': 'cpu', 'align': 'center',
                    children:[
                        {
                            title: '使用/总量',align:'center',
                            // node_stat: {cpu: {total: 2000, usage: 184}, memory: {total: 2991, usage: 1616}, pod: {total: 110},…}
                            render: (h,params) => {
                                let node_stat = params.row.node_stat
                                let cpu = node_stat['cpu']
                                let total= (cpu['total']/1000).toFixed(1)
                                let usage  = (cpu['usage']/1000).toFixed(1)
                                let value = usage+"c/"+total+"c"
                                return h('p',value)
                            }
                        },
                        {
                            title: '使用比例',key:'node_stat',sortable: true,
                            sortMethod:function(a,b,type) {
                                let a_cpu = a['cpu']
                                let a_total= a_cpu.total
                                let a_usage  = a_cpu.usage
                                let a_percent = a_usage/a_total

                                let b_cpu = b['cpu']
                                let b_total= b_cpu.total
                                let b_usage  = b_cpu.usage
                                let b_percent = b_usage/b_total

                                // console.log(a_percent+' '+b_percent+' '+type)
                                if (type == 'asc') {
                                    // return a > b? -1 : 1
                                    return a_percent < b_percent ? -1 : 1

                                } else {
                                    return a_percent > b_percent ? -1 : 1
                                    // return a > b? -1 : 1  
                                }
                            },

                            render: (h, params) => {
                                let node_stat = params.row.node_stat
                                let cpu = node_stat['cpu']
                                let total= cpu.total
                                let usage  = cpu.usage
                                let value = parseFloat((usage/total*100).toFixed(1))

                                if (value > 50 && value < 80){
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'active'}}, value+'%');
                                } else if (value >= 80){
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'wrong'}}, value+'%');
                                } else {
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'success'}}, value+'%');
                                }
                            }
                        },
                        {
                            title: '最低需求/最高需求',key: 'node_resources',
                            render: (h, params) => {
                                let node_resources = params.row.node_resources
                                let requests = node_resources['requests']
                                let limits = node_resources['limits']
                                let req_cpu = (requests['cpu']/1000).toFixed(2)
                                let limit_cpu = (limits['cpu']/1000).toFixed(2)
                                let value = req_cpu+"c/"+limit_cpu+"c"
                                
                                let total = params.row.node_stat.cpu.total*0.8
                                // console.log(req_cpu,total)
                                if(req_cpu > total || limit_cpu > total){
                                    // console.log("超请求")
                                    return h('p',{style:{'color':'red'}},value)
                                }else {
                                    return h('p',value)

                                }
                            }
                        },
                    ]

                },

                {
                    'title': '内存','align': 'center',
                    children:[
                        {
                            title: '使用/总量',align:'center',
                            render: (h,params) => {
                                let memory = params.row.node_stat.memory
                                let total= (memory.total/1024).toFixed(1)
                                let usage  = (memory.usage/1024).toFixed(1)
                                let value = usage+"G/"+total+"G"
                                return h('p',value)
                            }
                        },
                        {
                            title: '使用比例',key: 'usage_percent',
                            render: (h, params) => {
                                let memory = params.row.node_stat.memory
                                let total= memory.total
                                let usage  =memory.usage

                                let value = parseFloat((usage/total*100).toFixed(1))
                                if (value > 50 && value < 75){
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'active'}}, value+'%');
                                } else if (value >= 75){
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'wrong'}}, value+'%');
                                } else {
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'success'}}, value+'%');
                                }
                            }
                        },
                        {
                            title: '最低需求/最高需求',key: 'node_resources',
                            render: (h, params) => {
                                let node_resources = params.row.node_resources
                                let requests = node_resources['requests']
                                let limits = node_resources['limits']
                                let req_memory = (requests['memory']/1024).toFixed(1)
                                let limit_memory = (limits['memory']/1024).toFixed(1)
                                let value = req_memory+"G/"+limit_memory+"G"
                                
                                let total = (params.row.node_stat.memory.total/1024*0.8).toFixed(1)
                                // console.log(req_memory,total)
                                if(req_memory > total || limit_memory > total){
                                    return h('p',{style:{'color':'red'}},value)
                                }else {
                                    return h('p',value)
                                }
                            }
                        },
                    ]
                },
                {
                    'title': 'pod', 'align': 'center',
                    children:[
                        {
                            title: '使用/总',key: 'total',align:'center',
                            render: (h,params) => {
                                let pod = params.row.node_stat.pod
                                let total= pod.total
                                let usage  = pod.usage
                                let value = usage+"个/"+total+"个"
                                return h('p',value)
                            }
                        },
                        {
                            title: '使用比例',key: 'usage_percent',
                            render: (h, params) => {
                                // let value = params.row.usage_percent
                                let pod = params.row.node_stat.pod
                                let total= pod.total
                                let usage  = pod.usage
                                let value = parseFloat((usage/total*100).toFixed(1))
                                if (value > 20 && value < 50){
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'active'}}, value+'%');
                                } else if (value >= 50){
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'wrong'}}, value+'%');
                                } else {
                                    return  h('i-progress',{ props:{percent: value,'stroke-width': 15,'status':'success'}}, value+'%');
                                }
                            }
                        },
                    ]
                },

                {
                    title: '硬盘容量(G)',key: 'disk',align:'center',
                    render: (h, params) => {
                        let value = params.row.node_stat.disk.total+' G'
                        return h('p',value)
                    }
                },
            ],
            // 分页
            total_list: [],
            show_list: [],
            total: 0,
            pageSize: 10,
        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        refresh() {
            k8s_cluster_post(get_node_stat,{},{},30000).then(response => {
                this.total_list = response.data
                this.total = response.data.length
                if(this.total < this.pageSize) {
                    this.show_list = this.total_list
                }else {
                    // 修改改数据之后显示回到第一页的bug，改为停留在当前页
                    let currentPage = this.$refs.page.currentPage
                    let _start = (currentPage-1) * this.pageSize
                    let _end = currentPage * this.pageSize
                    this.show_list = this.total_list.slice(_start,_end)
                }
            })
        }
    },
    mounted: function() {
        this.refresh()
    }
}
</script>
<style scoped>

</style>